package com.ruoyi.project.product.controller;


import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.project.product.domain.Category;
import com.ruoyi.project.product.service.CategoriesService;
import com.ruoyi.project.system.domain.SysDept;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/system/categories")
public class CategoriesController extends BaseController {

    @Autowired
    private CategoriesService categoriesService;


    /**
     * 获取部门树列表
     */
    @GetMapping("/categoryTree")
    public AjaxResult deptTree(Category category)
    {
        return success(categoriesService.selectCategoriesTreeList(category));
    }
    /**
     * 获取部门列表
     */
    @GetMapping("/list")
    public AjaxResult list(Category Category)
    {
        List<Category> categoriesses = categoriesService.selectCategoriesList(Category);
        return success(categoriesses);
    }

    /**
     * 查询部门列表（排除节点）
     */
    @GetMapping("/list/exclude/{CategoriesId}")
    public AjaxResult excludeChild(@PathVariable(value = "CategoriesId", required = false) Long CategoriesId)
    {
        List<Category> categoriesses = categoriesService.selectCategoriesList(new Category());
        categoriesses.removeIf(d -> d.getCategoryId().intValue() == CategoriesId || ArrayUtils.contains(StringUtils.split(d.getAncestors(), ","), CategoriesId + ""));
        return success(categoriesses);
    }

    /**
     * 根据部门编号获取详细信息
     */
    @GetMapping(value = "/{CategoriesId}")
    public AjaxResult getInfo(@PathVariable Long CategoriesId)
    {
        categoriesService.checkCategoriesDataScope(CategoriesId);
        return success(categoriesService.selectCategoriesById(CategoriesId));
    }

    /**
     * 新增分类
     */
    @Log(title = "分类管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody Category category)
    {
        if (!categoriesService.checkCategoriesNameUnique(category))
        {
            return error("新增分类'" + category.getCategoryName() + "'失败，分类名称已存在");
        }
        category.setCreateBy(getUsername());
        return toAjax(categoriesService.insertCategories(category));
    }

    /**
     * 修改部门
     */
    @Log(title = "部门管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody Category category)
    {
        category.setUpdateBy(getUsername());
        return toAjax(categoriesService.updateCategories(category));
    }

    /**
     * 删除节点
     */
    @Log(title = "部门管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{categoriesId}")
    public AjaxResult remove(@PathVariable Long categoriesId)
    {
        if (categoriesService.hasChildBycategoriesId(categoriesId))
        {
            return warn("存在下级节点,不允许删除");
        }
        if (categoriesService.checkCategoriesExistUser(categoriesId))
        {
            return warn("分类存在文件,不允许删除");
        }
        categoriesService.checkCategoriesDataScope(categoriesId);
        return toAjax(categoriesService.deleteCategoriesById(categoriesId));
    }
}
